﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using EmotionRecognitionMessages;
using EmotionRecognitionEntities;

namespace EmotionRecognitionGui
{
    public partial class UserFeedBack : BasicForm
    {//
        GuiManager guiManager;
      
        Dictionary<Chunk,Case> solutions;
        List<Chunk> chunks;
        bool secondRun;
        bool isRefinementUsed;
        bool basic = true;
        public UserFeedBack()
        {
            try
            {
                guiManager = GuiManager.Instance;
                secondRun = false;
                InitializeComponent();
            }
            catch (Exception e)
            {
                MessageBox.Show("Error: " + e.ToString());
            }
        }
        public void UpdateSolutions(Dictionary<Chunk,Case> solutions)//transfer new solution to UserFeedBack GUI
        {
            this.solutions = solutions;
            buttonAccRetr.Enabled = false;
            if(solutions.Count == 1 && (solutions.Keys.ToArray()[0].ChunkId != -1))
                buttonAccRetr.Enabled = true;
            FillSolutionCategory();
        }
        //the function when reentering this GUI
        public void ReEnter(Dictionary<Chunk, Case> solutions, Picture currentImage, bool isRefinementUsed)
        {
            this.isRefinementUsed = isRefinementUsed;
            //if Refinement was run - disable basic and secondRun
            if (isRefinementUsed)
            {
                basic = false;
                secondRun = false;
            }
            buttonPostFeed.Enabled = false;
            UpdateSolutions(solutions);
            FillEmotionCategory();
            ShowSmallImage(currentImage);
            
        }

        private void ShowSmallImage(Picture currentImage)
        {
            imageBoxsmall.Image = currentImage.PictureImage;
            imageBoxsmall.SizeMode = PictureBoxSizeMode.Zoom;

        }

        protected void listBoxDiffEmotion_SelectedIndexChanged(object sender, EventArgs e)
        {
            buttonPostFeed.Enabled = true;
            labelAlert.Visible = false;
            textBoxNewEmotion.Clear();
        }
        
        protected void FillSolutionCategory()
        {
            listBoxRetrAns.Items.Clear();
            foreach (Chunk item in solutions.Keys)
            {
                if(item.ChunkId != -1)
                    listBoxRetrAns.Items.Add(item.Label);
            }
            
        }

        public void FillEmotionCategory()
        {
            listBoxDiffEmotion.Items.Clear();
            Messages msgs = new Messages();
            ReplyMsg reply = msgs.GetChunkslables();

            if (reply.MessageState == true)
            {
              //Get list of emotions from Replymsg
                List<Chunk> list = (List<Chunk>)reply.ParamArray[0];
                chunks = list; 
              foreach (Chunk item in list)
              {
                  if (solutions.Count == 1)
                  {
                      if (solutions.ContainsKey(item) == false) //if this is not the only solution
                          listBoxDiffEmotion.Items.Add(item.Label);//add to diff list box
                      //else dont add the chunk to list box
                  }
                  else
                  {
                      listBoxDiffEmotion.Items.Add(item.Label);
                  }
              }
            }
             
        }

        protected void buttonPostFeed_Click(object sender, EventArgs e)
        {
            Messages userFeedBackMsg = new Messages();
            //For Statistics- send true for retrieval update
            UpdateStatisticsManager(FeedBackType.Reject);
            if (textBoxNewEmotion.Text.Equals("") == false) //if new emotion is proposed
            {
                bool exists = false;
                foreach (Chunk chunk in chunks)
                {
                    if (textBoxNewEmotion.Text.Equals(chunk.Label) == true)
                        exists = true;//the emotion proposed exists in db
                }
                if (exists == false) //if the new emotion doesnt exist
                {
                    Messages msg = new Messages(); // create the new emotion in DB
                    msg.CreateNewChunk(textBoxNewEmotion.Text);
                    listBoxDiffEmotion.Items.Add(textBoxNewEmotion.Text); //fill the emotion list
                    userFeedBackMsg.ExecuteAdaptation(solutions, textBoxNewEmotion.Text, secondRun);
                }
                else
                {
                    labelAlert.Visible = true; //error label
                }
            }
            if (listBoxDiffEmotion.SelectedIndex != -1)
            {
                userFeedBackMsg.ExecuteAdaptation(solutions, (string)listBoxDiffEmotion.SelectedItem, secondRun);
            }
            guiManager.dataBaseImagesGui.RefreshPictureSolution();
            guiManager.SwitchForms(this,guiManager.dataBaseImagesGui);
            
        }

        protected void textBoxNewEmotion_TextChanged(object sender, EventArgs e)
        {
            if (textBoxNewEmotion.Text == "")
                buttonPostFeed.Enabled = false;
            else
                buttonPostFeed.Enabled = true;
            labelAlert.Visible = false;
            listBoxDiffEmotion.ClearSelected();
            
        }


         protected void buttonBack_Click(object sender, EventArgs e)
        {
            guiManager.SwitchForms(this, guiManager.dataBaseImagesGui);
           
        }
        protected void buttonAccRetr_Click(object sender, EventArgs e)
        {
           
            Messages userFeedBackMsg = new Messages();
            //For Statistics
            UpdateStatisticsManager(FeedBackType.Accept);
            userFeedBackMsg.ExecuteAdaptation(solutions, (string)listBoxRetrAns.Items[0].ToString(), secondRun);
            guiManager.dataBaseImagesGui.RefreshPictureSolution();
            guiManager.SwitchForms(this, guiManager.dataBaseImagesGui);
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void listBoxRetrAns_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void panel2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }

        private void groupBox2_Enter_1(object sender, EventArgs e)
        {

        }

        private void checkBoxRound_CheckedChanged(object sender, EventArgs e)
        {
            secondRun = (secondRun == false) ? true : false;
            if (secondRun)
                basic = false;
        }


        private void UpdateStatisticsManager(FeedBackType feedbackType)
        {
            Messages userFeedBackMsg = new Messages();
            if (secondRun == true)
               userFeedBackMsg.UpdateStatisticsManager(StatisticsType.SecondIndex, feedbackType);
            if (basic==true)
               userFeedBackMsg.UpdateStatisticsManager(StatisticsType.Basic, feedbackType);
             if (isRefinementUsed==true )
              userFeedBackMsg.UpdateStatisticsManager(StatisticsType.Refinement, feedbackType);
        }

    }
}
